package com.fs.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.controller.BaseController;
import com.fs.mapper.GoodsFavouriteMapper;
import com.fs.pojo.Goods;
import com.fs.pojo.GoodsFavourite;
import com.fs.service.GoodsFavouriteService;
import com.fs.vo.CommRequestVO;
import com.fs.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class GoodsFavouriteServiceImpl extends BaseController implements GoodsFavouriteService {

    @Autowired
    private GoodsFavouriteMapper goodsFavouriteMapper;


//    @Override
//    @Transactional
//    public ResultVO add(GoodsFavouriteEntity entity) {
//        goodsFavouriteDao.insert(entity);
//        return ResultVO.success();
//    }

    @Override
    @Transactional
    public ResultVO edit(GoodsFavourite entity) {
        goodsFavouriteMapper.updateById(entity);
        return ResultVO.success();
    }

    @Override
    public ResultVO list(CommRequestVO request) {
        if (request == null) return ResultVO.fail();

        int pageNum = request.getPageNum();
        int pageSize = request.getPageSize();

        if (pageNum < 1) pageNum = 1;
        if (pageSize < 1) pageNum = 10;

        Page<GoodsFavourite> page = new Page(pageNum, pageSize);

        Integer userId = getUserId();

        int offsetNum = (pageNum - 1) * pageSize;
//        QueryWrapper<GoodsFavouriteEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id", getUserId())
//                .eq("status", true)
//                .orderByDesc("id");
//        page = goodsFavouriteDao.selectPage(page, queryWrapper);
//        request.setTotal(page.getTotal()).setRows(page.getRecords());

        List<Goods> list=goodsFavouriteMapper.getGoodsFav(userId,offsetNum,pageSize);
        Long total=goodsFavouriteMapper.getGoodsFavTotal(userId);
        request.setTotal(total).setRows(list);

        return ResultVO.success(request);
    }

    @Override
    @Transactional
    public ResultVO delById(Integer id) {
        goodsFavouriteMapper.deleteById(id);
        return ResultVO.success();
    }

    @Transactional
    @Override
    public ResultVO fav(Integer userId, Integer goodsId) {
        if (!userId.equals(getUserId())) {
            return ResultVO.fail("账号有误");
        }

        Date now = new Date();

        QueryWrapper<GoodsFavourite> wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId)
                .eq("goods_id", goodsId);
        GoodsFavourite goodsFavouriteEntity = goodsFavouriteMapper.selectOne(wrapper);
        if (goodsFavouriteEntity == null) {
            GoodsFavourite entity = new GoodsFavourite();
            entity.setUserId(userId)
                    .setGoodsId(goodsId)
                    .setStatus(true)
                    .setCreated(now)
                    .setUpdated(now);
            goodsFavouriteMapper.insert(entity);
            return ResultVO.success();
        } else {
            if (goodsFavouriteEntity.getStatus()) {
                goodsFavouriteEntity.setStatus(false).setUpdated(now);
                goodsFavouriteMapper.updateById(goodsFavouriteEntity);
                return ResultVO.success("已取消收藏", null);
            } else {
                goodsFavouriteEntity.setStatus(true).setUpdated(now);
                goodsFavouriteMapper.updateById(goodsFavouriteEntity);
                return ResultVO.success("已添加收藏", null);
            }
        }


    }

    @Transactional
    @Override
    public ResultVO clearAll() {
        goodsFavouriteMapper.clearAll(getUserId());
        return ResultVO.success();
    }
}
